﻿using Developer.Code;
using Developer.Entity;
using Developer.Service;
using Super.Framework.Common;
using Super.Framework.Common.Web;
using Super.Framework.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Developer.Web.Areas.Admin.Controllers
{
    public class OrderController : BaseController
    {
        // GET: Admin/Order
        public ActionResult RepairIndex()
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;
            var users = RepairOrderService.GetAll().Where(a => a.State > -2);

            if (key.IsNotNullOrEmpty())
            {
                users = users.Where(a => a.RepairOrderDetail.UserPhone.Contains(key));
                ViewBag.Name = key;
            }
            var items = users.OrderByDescending(u => u.CreateTime).OrderBy(u => u.State)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex);

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            return View(items);
        }

        public ActionResult CycleIndex()
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;
            var users = CycleOrderService.GetAll().Where(a => a.State > -2);

            if (key.IsNotNullOrEmpty())
            {
                users = users.Where(a => a.CycleOrderDetail.UserPhone.Contains(key));
                ViewBag.Name = key;
            }
            var items = users.OrderByDescending(u => u.CreateTime).OrderBy(u => u.State)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex);

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            return View(items);
        }
        
        [HttpPost]
        public ActionResult SetRepairOrderDone()
        {
            var userId = Query.Value("Id").ToInt();
            if (userId > 0)
            {
                var user = RepairOrderService.GetAll().FirstOrDefault(a => a.Id == userId);
                if (user != null)
                {
                    if (user.State == 0)
                    {
                        user.State = 1;
                        RepairOrderService.Change(user);
                        ShowMsg("操作成功", MessageItemType.Right);
                        return Redirect("/admin/user/index");
                    }
                    user.State = 0;
                    RepairOrderService.Change(user);
                    ShowMsg("操作成功", MessageItemType.Right);
                    return Redirect("/admin/user/index");
                }
                ShowMsg("操作失败", MessageItemType.Error);
                return Redirect("/admin/user/index");
            }
            ShowMsg("操作失败", MessageItemType.Error);
            return Redirect("/admin/user/index");
        }
        [HttpPost]
        public ActionResult SetCycleOrderDone()
        {
            var userId = Query.Value("Id").ToInt();
            if (userId > 0)
            {
                var user = CycleOrderService.GetAll().FirstOrDefault(a => a.Id == userId);
                if (user != null)
                {
                    if (user.State == 0)
                    {
                        user.State = 1;
                        CycleOrderService.Change(user);
                        ShowMsg("操作成功", MessageItemType.Right);
                        return Redirect("/admin/user/index");
                    }
                    user.State = 0;
                    CycleOrderService.Change(user);
                    ShowMsg("操作成功", MessageItemType.Right);
                    return Redirect("/admin/user/index");
                }
                ShowMsg("操作失败", MessageItemType.Error);
                return Redirect("/admin/user/index");
            }
            ShowMsg("操作失败", MessageItemType.Error);
            return Redirect("/admin/user/index");
        }
    }
}